<?php
namespace Admin\Controller;
use Lib\Api\OrganizationSettingApi;
use Think\Controller;
use Think\Model;
class OrganizationSettingController extends Controller {
    private $modelName = 'OrganizationSetting';

    public function index() {
        $this->display();
    }

    public function tolist() {
        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $organization_id = session('admin_id');
        $where["organization_id"] = $organization_id;
        $list = OrganizationSettingApi::getList($where, $page_num, $page_size);
        $total = OrganizationSettingApi::getTotal($where);
        $rel['data'] = pageHelper($page_size, $page_num, $list, $total);
        $this->ajaxReturn($rel);
    }

    public function getOrganizationSettingInfo() {
        $id = $_POST['id'];
        $organizationSetting = OrganizationSettingApi::getInfo(array("id"=>$id));
        $this->ajaxReturn($organizationSetting);
    }

    public function addoreditdata() {
        $modelObj = M($this->modelName);
        $ajaxdata['code'] = 0;
        $id = $_POST['id'];
        $data = $modelObj->create($_POST);

        //数据校验
        if (empty($data['name'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写公众号名称';
            $this->ajaxReturn($ajaxdata);
        }

        if (empty($data['wx_appid'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写微信AppID';
            $this->ajaxReturn($ajaxdata);
        }

        if (empty($data['wx_secret'])) {
            $ajaxdata['code'] = -1;
            $ajaxdata['msg'] = '未填写微信Secret';
            $this->ajaxReturn($ajaxdata);
        }

        if (empty($_POST['id'])) {
            //添加的数据是否存在
            $where['_string'] = 'name="'.$data['name'].'" OR wx_appid="'.$data['wx_appid'].'" OR wx_secret="'.$data['wx_secret'].'"';
            $result = OrganizationSettingApi::getTotal($where);
            if ($result == 0) {
                //数据不存在，添加数据
                $data["organization_id"] = session('admin_id');
                $id = OrganizationSettingApi::add($data);
                $ajaxdata['msg'] = '添加成功';
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '添加失败，公会号已存在';
            }
        } else {
            $where['_string'] = '(name="'.$data['name'].'" OR wx_appid="'.$data['wx_appid'].'" OR wx_secret="'.$data['wx_secret'].'") and id not in('.$data['id'].')';
            $result = OrganizationSettingApi::getTotal($where);
            if ($result == 0) {
                OrganizationSettingApi::save($data);
                $ajaxdata['code'] = 1;
                $ajaxdata['msg'] = "公会号修改成功";
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = "公会号修改失败，公会号已存在";
            }
        }
        $this->ajaxReturn($ajaxdata);
    }

    // 更改经纪人状态
    public function changeState() {
        OrganizationSettingApi::saveByCondition(array("state"=>1), array("state"=>2));
        $model = M($this->modelName);
        $this->ajaxReturn(OrganizationSettingApi::save($model->create($_POST, Model::MODEL_UPDATE)));
    }
}